package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.server.UserService;
import io.jsonwebtoken.Claims;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/loginReginfo")
    public void save(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
        //在拦截器已处理，验证不通过直接返回了
        //利用工具类解析客户端token，如果校验密钥正确就返回true
//        boolean token1 = JwtUtils.verifyToken(token);
//        if(!token1){
//            throw new RuntimeException("用户未登录");
//        }
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = claims.get("id",Long.class); //此处获取Long类型标注类型
        //在拦截器已解析token获取到了user，可取出id
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        userService.saveUserInfo(userInfo);
    }
    @PostMapping("/loginReginfo/head")
    public void userInfoHead(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
//        boolean b = JwtUtils.verifyToken(token);
////        if(!b){
////            throw new RuntimeException("用户未登录");
////        }
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = claims.get("id", Long.class);
        Long userId = UserHolder.getUserId();

        userService.head(userId,headPhoto);
    }
}
